<template>
  <!--
        数量控制器：
            只需要负责对数据的逻辑处理，而不需要去管其他东西（是在那种情况被引用）
     -->
  <div class="number-manger">
    <span class="number-manger-less" @click="onLessClick" :class="{'number-manger-disable' : number === 1}">-</span>
    <span class="number-manger-number">{{ number }}</span>
    <span class="number-manger-add" @click="onAddClick">+</span>
  </div>
</template>

<script>
export default {
  props: {
    defaultNumber: {
      type: Number,
      default: 1
    }
  },
  data: function () {
    return {
      // 数量数据源项
      number: 1
    }
  },
  methods: {
    /**
     * 减号按钮点击事件
     */
    onLessClick: function () {
      if (this.number === 1) {
        return
      }
      this.number -= 1
    },
    /**
     * 加号按钮点击事件
     */
    onAddClick: function () {
      this.number += 1
    }

  },
  /**
   * 监听 defaultNumber
   */
  watch: {
    defaultNumber: function (newV) {
      this.number = newV
    },
    /**
     * 监听数量变化
     */
    number: function (newV) {
      this.$emit('onChangeNumber', newV)
    }
  }
}
</script>

<style lang="scss" scoped>
    @import '@css/style.scss';

    .number-manger {
        display: flex;
        font-size: $infoSize;
        height: px2rem(20);
        line-height: px2rem(20);

        span {
            width: px2rem(30);
            text-align: center;
            display: inline-block;
            font-weight: 500;
        }

        &-number {
            background-color: $bgColor;
            padding: 0 px2rem(4);
        }

        &-disable {
            color: $hintColor;
        }
    }
</style>
